package com.lr.frame.common.colorful.reflect.javassist;

import java.io.File;

import com.lr.frame.common.colorful.reflect.bean.Programmer;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.CtNewMethod;

public class JavassistGenerator {

	public static void main(String[] args) throws Exception {
		ClassPool pool = ClassPool.getDefault();
		String name = Programmer.class.getCanonicalName() + "Javassit";
		CtClass declaring = pool.makeClass(name);
		CtMethod ctm = CtNewMethod.make("public void code(){}", declaring);
		ctm.insertBefore("System.out.println(\"hello word javassit\");");
		declaring.addMethod(ctm);
		declaring.writeFile(new File(".").getCanonicalPath() + "\\target\\classes\\");
		Class<?> javassitClass = Class.forName(name);
		Object obj = javassitClass.newInstance();
		javassitClass.getMethod("code", null).invoke(obj, null);
	}
}
